package com.hr.reactive.flux;

import com.google.common.collect.Lists;
import com.hr.reactive.common.Utils;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * Flux -> Mono
 * Mono -> Flux
 *
 * @author Administrator
 */
public class Lec07FluxToMono {
    public static void main(String[] args) {
        save(getUserName(3).flux());
        Flux<Integer> flux = Flux.range(1, 3);
        flux.next().subscribe(Utils.subscriber());
        Mono.from(flux).subscribe(Utils.subscriber());
    }


    private static void monoToFlux() {
        var mono = getUserName(1);
        save(Flux.from(mono));
    }

    private static Mono<String> getUserName(int userId) {
        return switch (userId) {
            case 1 -> Mono.just("sam");
            case 2 -> Mono.empty();//null
            default -> Mono.error(new RuntimeException("error"));
        };
    }

    private static void save(Flux<String> flux) {
        flux.subscribe(Utils.subscriber());
    }
}
